home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / Intros / OffScreenIntro / OffScreenIntro.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  4.2 KB  |  186 lines  |  [TEXT/CWIE]

  1. /*
  2. **    OffScreenIntro.c
  3. */
  4.  
  5. #include "agl.h"
  6.  
  7. #include <QDOffscreen.h>
  8. #include <Fonts.h>
  9. #include <MacWindows.h>
  10.  
  11. #define WIDTH  300
  12. #define HEIGHT 200
  13.  
  14. /*
  15. ** OpenGL Setup
  16. */
  17. static AGLContext setupAGL(void *baseaddr, GLsizei rowbytes, GLsizei pixelsize)
  18. {
  19.     AGLPixelFormat fmt;
  20.     AGLContext     ctx;
  21.     GLboolean      ok;
  22.     GLint          attrib[] = { AGL_RGBA, AGL_PIXEL_SIZE, 0, AGL_OFFSCREEN, AGL_NONE };
  23.  
  24.     /* Set the pixel size attribute */
  25.     attrib[2] = pixelsize;
  26.  
  27.     /* Choose an rgb pixel format */
  28.     fmt = aglChoosePixelFormat(NULL, 0, attrib);
  29.     if(fmt == NULL) return NULL;
  30.  
  31.     /* Create an AGL context */
  32.     ctx = aglCreateContext(fmt, NULL);
  33.     if(ctx == NULL) return NULL;
  34.  
  35.     /* Attach the off screen area to the context */
  36.     ok = aglSetOffScreen(ctx, WIDTH, HEIGHT, rowbytes, baseaddr);
  37.     if(!ok) return NULL;
  38.     
  39.     /* Make the context the current context */
  40.     ok = aglSetCurrentContext(ctx);
  41.     if(!ok) return NULL;
  42.  
  43.     /* The pixel format is no longer needed */
  44.     aglDestroyPixelFormat(fmt);
  45.  
  46.     return ctx;
  47. }
  48.  
  49. /*
  50. ** OpenGL Cleanup
  51. */
  52. static void cleanupAGL(AGLContext ctx)
  53. {
  54.     aglSetCurrentContext(NULL);
  55.     aglSetDrawable(ctx, NULL);
  56.     aglDestroyContext(ctx);
  57. }
  58.  
  59. /*
  60. ** OpenGL Drawing
  61. */
  62. static void drawGL(void)
  63. {
  64.     /* Clear the color buffer */
  65.     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
  66.     glClear(GL_COLOR_BUFFER_BIT);
  67.     
  68.     /* Draw a smooth shaded polygon */
  69.     glBegin(GL_POLYGON);
  70.     glColor3d(1.0, 1.0, 0.0);
  71.     glVertex3d( 0.8,  0.8, 0.0);
  72.     glColor3d(0.0, 1.0, 1.0);
  73.     glVertex3d( 0.8, -0.8, 0.0);
  74.     glColor3d(1.0, 0.0, 1.0);
  75.     glVertex3d(-0.8, -0.8, 0.0);
  76.     glColor3d(1.0, 0.0, 0.0);
  77.     glVertex3d(-0.8,  0.8, 0.0);
  78.     glEnd();
  79.  
  80.     /* Ensure completion */
  81.     glFinish();
  82. }
  83.  
  84. /*
  85. ** Setup the front colormap
  86. ** If we're running 8 bit depth, we must set the front colormap to a 332
  87. ** map so that CopyBits will translate the colors correctly.  by default,
  88. ** a 332 map is used by the 8 bit renderers for offscreen drawables.
  89. */
  90. static void set332CMap(ColorSpecPtr ctab)
  91. {
  92.     GLfloat a4[4];
  93.     GLfloat a8[8];
  94.     short i; 
  95.  
  96.     for(i = 0; i < 3; i++)
  97.         a4[i] = 65535.0f * 0.333333333333f * i;
  98.     a4[3] = 65535.0f;
  99.             
  100.     for(i = 0; i < 7; i++)
  101.         a8[i] = 65535.0f * 0.142857142857f * i;
  102.     a8[7] = 65535.0f;
  103.             
  104.     for(i = 0; i < 256; i++)
  105.     {
  106.         ctab[i].value     = i;
  107.         ctab[i].rgb.blue  = (unsigned short) a4[i >> 6];
  108.         ctab[i].rgb.green = (unsigned short) a8[(i >> 3) & 0x07];
  109.         ctab[i].rgb.red   = (unsigned short) a8[i & 0x07];
  110.     }
  111. }
  112.  
  113. /*
  114. ** Macintosh main routine
  115. */
  116. int main(int argc, char *argv[])
  117. {
  118.     Rect rect;
  119.     WindowPtr win;
  120.     AGLContext ctx;
  121.     GWorldPtr g_world;
  122.     QDErr qdErr;
  123.     PixMapHandle pixMap;
  124.     int rowbytes;
  125.     void *baseaddr;
  126.     long pixelsize;
  127.  
  128.     /* Initialize Macintosh system */
  129.     InitGraf(&qd.thePort);
  130.     InitFonts();
  131.     InitWindows();
  132.  
  133.     /* Find the depth of the main screen */
  134.     pixelsize = (*(*GetMainDevice())->gdPMap)->pixelSize;
  135.  
  136.     /* Create the offscreen gworld */
  137.     SetRect(&rect, 0, 0, WIDTH, HEIGHT);
  138.     qdErr = NewGWorld(&g_world, (short) pixelsize, &rect, NULL, NULL, useTempMem);
  139.     if(qdErr || !g_world) return 1;
  140.  
  141.     /* Lock pixmap handle and get its specifications */
  142.     pixMap = GetGWorldPixMap(g_world);
  143.     LockPixels(pixMap);
  144.     baseaddr = GetPixBaseAddr(pixMap);
  145.     rowbytes = (**pixMap).rowBytes & 0x7FFF;
  146.  
  147.     /* Setup the AGL context */
  148.     ctx = setupAGL(baseaddr, rowbytes, pixelsize);
  149.     if(!ctx) return 1;
  150.  
  151.     /* Do the OpenGL drawing */
  152.     drawGL();
  153.     
  154.     /* Cleanup the OpenGL context */
  155.     cleanupAGL(ctx);
  156.  
  157.     /* Create a window */
  158.     SetRect(&rect, 50, 50, 50 + WIDTH, 50 + HEIGHT);
  159.     win = NewCWindow(NULL, &rect, "\pOffScreen Intro", false,
  160.                                 plainDBox, (WindowPtr) -1L, true, 0L);
  161.     if(win == NULL) return 1;
  162.     ShowWindow(win);
  163.     
  164.     /* If we're running 8 bit depth, we must set the back colormap to a 332
  165.     ** map so that CopyBits will translate the colors correctly.  by default,
  166.     ** a 332 map is used by the 8 bit renderers for offscreen drawables. */
  167.     if(pixelsize == 8)
  168.         set332CMap((*(*g_world->portPixMap)->pmTable)->ctTable);
  169.     
  170.     /* Copy the image to the window */
  171.     SetPort((GrafPtr) win);
  172.     SetRect(&rect, 0, 0, 300, 200);
  173.     CopyBits(&((GrafPtr) g_world)->portBits, &((GrafPtr) win)->portBits,
  174.             &rect, &rect, srcCopy, NULL);
  175.  
  176.     /* Wait until the mouse button is pressed */
  177.     while(!Button()) {}
  178.  
  179.     /* Destroy the window and GWorld */
  180.     UnlockPixels(pixMap);
  181.     DisposeGWorld(g_world);
  182.     DisposeWindow((WindowPtr) win);
  183.     
  184.     return 0;
  185. }
  186.